/*
 * @Author: your name
 * @Date: 2021-08-18 15:11:12
 * @LastEditTime: 2021-10-27 11:42:50
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \cscec-pms-admin\src\core\directives\btnPermission\btnPermission.js
 */
import store from '@/store'

// 项目管理 业务执行prma_seex 业务管控prma_buco 业务查询prma_buqu
const authCode = ['prma_seex', 'prma_buco', 'prma_buqu']
const authPrefix = ['seex_', 'buco_', 'buqu_']
const whiteCode = ['loo', 'moi', 'der', 'exp', 'pri', 'dow', 'dln','app','ann','exa', 'all', 'bat']

function getQueryString(name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  var r = window.location.search.substr(1).match(reg)
  if (r != null) return unescape(r[2])
  return null
}

export default {
  inserted(el, binding) {
    if(process.env?.VUE_APP_disabledBtnAuth === 'disabled') {
      return
    }
    let { value } = binding
    if(!value) {
      return
    }
    const btnPermissionList = store.getters.btnPermission
    const activeCodes = store.state.user.activeCodes[1];//getQueryString('query')
    if (authCode.includes(activeCodes)) {
      if (Array.isArray(value)) {
        // 兼容数组权限
        if (authCode.includes(activeCodes)) {
          const idx = authCode.findIndex(item => item === activeCodes)
          if (idx == 2) {
            let flag = false
            for (let i = 0; i < whiteCode.length; i++) {
              const ele = whiteCode[i]
              if (value[0].code.indexOf(ele) > -1) {
                flag = true
                break
              }
            }
            if (!flag) {
              el.parentNode && el.parentNode.removeChild(el)
            }
            return
          }
        }
        for (let i = 0; i < value.length; i++) {
          const isIdent = activeCodes === value[i].ident ? true : false
          if (isIdent) {
            const index = btnPermissionList.findIndex(item => item.code === value[i].code)
            if (index < 0) {
              el.parentNode && el.parentNode.removeChild(el)
            }
            break
          }
        }
        return
      }
      if (authCode.includes(activeCodes)) {
        const idx = authCode.findIndex(item => item === activeCodes)
        if (idx > -1 && idx !== 2) {
          value = authPrefix[idx] + value
        } else {
          // 匹配到业务查询了
          let flag = false
          for (let i = 0; i < whiteCode.length; i++) {
            const ele = whiteCode[i]
            if (value.indexOf(ele) > -1 && value.indexOf("delall") < 0 ) {
              flag = true
              break
            }
          }
          if (!flag) {
            console.log(value, 'node2')
            el.parentNode && el.parentNode.removeChild(el)
          }
          return
        }
      }
    }
    const index = btnPermissionList.findIndex(item => item.code === value)
    if (index < 0) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  },
  // 处理数据变更后，没有更新权限按钮的bug
  componentUpdated(el, binding) {
    if(process.env?.VUE_APP_disabledBtnAuth === 'disabled') {
      return
    }
    let { value } = binding
    if(!value) {
      return
    }
    const btnPermissionList = store.getters.btnPermission
    const activeCodes = store.state.user.activeCodes[1];//getQueryString('query')
    if (authCode.includes(activeCodes)) {
      // 如果需要,可以手动传参
      if (Array.isArray(value)) {
        // 兼容数组权限
        if (authCode.includes(activeCodes)) {
          const idx = authCode.findIndex(item => item === activeCodes)
          if (idx == 2) {
            let flag = false
            for (let i = 0; i < whiteCode.length; i++) {
              const ele = whiteCode[i]
              if (value[0].code.indexOf(ele) > -1) {
                flag = true
                break
              }
            }
            if (!flag) {
              el.parentNode && el.parentNode.removeChild(el)
            }
            return
          }
        }
        for (let i = 0; i < value.length; i++) {
          const isIdent = activeCodes === value[i].ident ? true : false
          if (isIdent) {
            const index = btnPermissionList.findIndex(item => item.code === value[i].code)
            if (index < 0) {
              el.parentNode && el.parentNode.removeChild(el)
            }
            break
          }
        }
        return
      }
      // 自动拼接前缀
      if (authCode.includes(activeCodes)) {
        const idx = authCode.findIndex(item => item === activeCodes)
        if (idx > -1 && idx !== 2) {
          value = authPrefix[idx] + value
        } else {
          // 匹配到业务查询了
          let flag = false
          for (let i = 0; i < whiteCode.length; i++) {
            const ele = whiteCode[i]
            if (value.indexOf(ele) > -1 && value.indexOf("delall") < 0) {
              flag = true
              break
            }
          }
          if (!flag) {
            console.log(value, 'node')
            el.parentNode && el.parentNode.removeChild(el)
          }
          return
        }
      }
    }
    const index = btnPermissionList.findIndex(item => item.code === value)
    // tab切换的时候隐藏页面的按钮会触发指令更新，移除后无法再添加进来，因此改为显示隐藏
    if (index < 0) {
      // el.parentNode && el.parentNode.removeChild(el)
      el.style.setProperty('display', 'none')
    }else{
      el.style.removeProperty('display')
    }
  }
}
